//
//  BBReawardRecodViewController.m
//  BlockBrowser
//
//  Created by Mac on 2018/4/18.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBReawardRecodViewController.h"
#import "UITableView+STNoresult.h"
#import "BBReawardRecordTableViewCell.h"
@interface BBReawardRecodViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**< 数据源 */
@property(nonatomic, assign) NSInteger                          page;/**< 默认page = 1 */
@end

@implementation BBReawardRecodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收益记录";
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无收益" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.page = 1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page =1;
        [self sendRecordListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = self.page + 1;
        [self sendRecordListRequest];
    }];
    // Do any additional setup after loading the view.
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BBReawardRecordTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BBReawardRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BBReawardRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    NSDictionary * dic = self.dataSouce[indexPath.row];
    if ([dic.allKeys containsObject:@"dateline"]) {
        cell.timeLable.text = [dic[@"dateline"] description];
    }
    if ([dic.allKeys containsObject:@"content"]) {
        cell.titleLable.text = [dic[@"content"] description];
    }
    if ([dic.allKeys containsObject:@"coin"]) {
        NSString * coin = [dic[@"coin"] description];
        cell.moneyLable.text = [NSString stringWithFormat:@"+ ￥%@",coin];
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)sendRecordListRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:@(_page) forKey:@"page"];
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/getAwardHistory"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSArray * array = responseObject[@"page"][@"list"];
                                                 if (self.page == 1) {
                                                     self.dataSouce = [NSMutableArray new];
                                                 }
                                                
                                                 for (NSDictionary * modelDic in array) {
                                                     [self.dataSouce addObject:modelDic];
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer  endRefreshing];
                                                
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
